package com.FastTorchPro;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.preference.PreferenceManager;
import android.widget.RemoteViews;

public class FastTorchBigWidget extends AppWidgetProvider {

	public static String ACTION_WIDGET_BIG_RECEIVER = "ActionReceiverWidgetBig";
	 
	@Override
	  public void onEnabled(Context context) {
	    super.onEnabled(context);
	 
	  }
	
	@Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
         //Создаем новый RemoteViews
         RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget);
         
         //Подготавливаем Intent дл�? Broadcast
         Intent active = new Intent(context, FastTorchBigWidget.class);
         active.setAction(ACTION_WIDGET_BIG_RECEIVER);
        
         context.startService(new Intent(context, RotaitionService.class));
         
//         
//         Toast toast = Toast.makeText(context, 
//        			"Update Vi", Toast.LENGTH_SHORT); 
//       			toast.show();
       			

         //�?оздаем наше �?обытие
         PendingIntent actionPendingIntent = PendingIntent.getBroadcast(context, 0, active, 0);

         //реги�?трируем наше �?обытие
         remoteViews.setOnClickPendingIntent(R.id.widgetImage, actionPendingIntent);

         //обновл�?ем виджет
         appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);

	}
	
	
	
	 @Override
     public void onReceive(Context context, Intent intent) {

          //Ловим наш Broadcast, провер�?ем и выводим �?ообщение
          final String action = intent.getAction();
          if (ACTION_WIDGET_BIG_RECEIVER.equals(action)) {
        	  
        	  GlobalLogic.prefs = PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext());
        	  
        	  if (Integer.parseInt(GlobalLogic.prefs.getString("def_Lihgt", "0"))!=0){
        		  
        		  Intent runs = new Intent(context.getApplicationContext(), MainFastTorchActivity.class);
				  
 				 runs.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
 				context.getApplicationContext().startActivity(runs);
        	  }
        	  else{
        		  GlobalLogic.runedFrom=GlobalLogic.RUN_FROM_WIDGET;
        		  GlobalLogic.onOffTorch(context);
        	  }
        	 
          } 
          if (action.equals("android.appwidget.action.APPWIDGET_UPDATE")){
        	  
        	  AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
        	  int[] appWidgetIds =
        	      appWidgetManager.getAppWidgetIds(new ComponentName(context, this.getClass()));
        	  
        	 
        	  
        	  RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget);
        	  int id=R.drawable.small_off_w;
        	  if (GlobalLogic.curentMode==GlobalLogic.ON_LED_MODE) id=R.drawable.small_on_w;
        	  
        	  remoteViews.setImageViewResource(R.id.widgetImage, id);       		  
        	  appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
        	  
        	  onUpdate(context,appWidgetManager,appWidgetIds);
        	  
        	  if (GlobalLogic.curentMode==GlobalLogic.EXIT_MODE){
          		GlobalLogic.readyToExitBig=true;
          		GlobalLogic.closeApp(context);
          	  }
        	
          }
          super.onReceive(context, intent);
    }
}
